package cn.com.dhcc.app.pub.module.resmgr.service;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.dhcc.app.core.base.log.LogType;
import cn.com.dhcc.app.core.boot.AppInterceptor;
import cn.com.dhcc.app.core.component.log.Log;

/**
 * 根据某个类制动生成resource里面的内容
 * @日期：2013-2-26上午9:01:34
 * @作者：乔兵
 * @版权所有：东华软件
 * @版本：1.0
 */
public class CreateResourceTxt {

	public static Logger logger = Log.getLogger(LogType.SYSINNER);
	public static  List<String> generalActionXmlByClass(Class<?> clazz){
		if(clazz==null)return null;
		List<String> rs = new ArrayList<String>();
		RequestMapping classmapping = clazz.getAnnotation(RequestMapping.class);
		String pre = "";
		if(classmapping!=null){
			pre = classmapping.value()[0]==null?"" : classmapping.value()[0];
		}
		String action = "";
		Method[] methods = clazz.getMethods();
		for (Method handlerMethod : methods) {
			RequestMapping methodmapping = handlerMethod.getAnnotation(RequestMapping.class);
			if(methodmapping!=null){
				action = methodmapping.value()[0]==null?"" : methodmapping.value()[0];
				String cacheUrl = AppInterceptor.convertCacheUrl(pre,action);
				rs.add(cacheUrl);
			}
		}
		return rs;
	}
	public static List<Class<?>> getModule(String basePackage) {
		final List<Class<?>> classes = new ArrayList<Class<?>>();
		ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
		provider.addIncludeFilter(new TypeFilter() {
			public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
					throws IOException {
				if (metadataReader.getAnnotationMetadata().getAnnotationTypes().contains(Controller.class.getCanonicalName())) {
					String className = metadataReader.getClassMetadata().getClassName();
					try {
						classes.add(ClassUtils.forName(className,ClassUtils.getDefaultClassLoader()));
					} catch (Exception e) {
						logger.error("注册module[" + className + "]出错，查看是否有无参数public的构造函数", e);
					}
					//System.out.println("找到  module >>>>" + className);
					return true;
				}
				return false;
			}
		});
		provider.findCandidateComponents(basePackage);
		return classes;
	}
	public static void main() {
		String basePackage = "cn.gov.mwr.si.*.*.controller";
		List<Class<?>> classes = getModule(basePackage);
		Map<String,List<String>> allUrl = new HashMap<String,List<String>>();
		for (Iterator iterator = classes.iterator(); iterator.hasNext();) {
			Class class1 = (Class) iterator.next();
			allUrl.put(class1.getCanonicalName(), generalActionXmlByClass(class1));
		}
		for (Iterator iterator = allUrl.keySet().iterator(); iterator.hasNext();) {
			String c = (String) iterator.next();
			System.out.println("----------"+c);
			List<String> cu  = allUrl.get(c);
			for (Iterator iterator2 = cu.iterator(); iterator2.hasNext();) {
				String u = (String) iterator2.next();
				System.out.println(u);
			}
		}
		
	}
}
